package com.lovo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.*;
import com.lovo.service.RoleService;
import com.lovo.service.UserService;
import com.lovo.util.MyUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    RoleService rs = new RoleServiceImpl();
    @Override
    public boolean register(User u) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserMapper.insertSelective";
        int n = sqlSession.insert(stmt, u);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean deleteUser(String userId) {
        int id = Integer.parseInt(userId);
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserMapper.deleteByPrimaryKey";
        int n = sqlSession.delete(stmt, id);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean updateUser(User u) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserMapper.updateByPrimaryKeySelective";
        int n = sqlSession.update(stmt, u);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean hasRoles(int userId, Integer[] roleIds) {
        boolean flag = false;
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserRoleMapper.insertSelective";
        String stmt1 ="com.lovo.dao.UserRoleMapper.deleteByFkUserId";
        sqlSession.delete(stmt1, userId);
        for (int i = 0; i<roleIds.length;i++){
            UserRole ur = new UserRole();
            ur.setFkUserId(userId);
            ur.setFkRoleId(roleIds[i]);
            int n = sqlSession.insert(stmt, ur);
            if (n>0){
                flag = true;
            }else {
                flag = false;
            }
        }
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    @Override
    public int[] getRoleIds(int userId) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserRoleMapper.findAllByFkUserId";
        List<UserRole> userRoles = sqlSession.selectList(stmt, userId);
        int [] roleIds = new int [userRoles.size()];
        for (int i = 0; i < userRoles.size(); i++){
            roleIds[i] = userRoles.get(i).getFkRoleId();
        }
        sqlSession.commit();
        sqlSession.close();
        return roleIds;
    }

    public  List <Menu> getMenuList(List<String>authorityList){
        List<Menu> menus = new ArrayList<>();
        if (authorityList.contains("门店申请权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("1-1","门店申请","/storeApply","storeApply");
            list.add(childrenMenu);
            Menu menu = new Menu("1","门店申请","el-icon-circle-plus",list,"storeApply");
            menus.add(menu);
        }
        if(authorityList.contains("门店管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("2-1","门店管理","/store","store");
            list.add(childrenMenu);
            Menu menu = new Menu("2","门店管理","el-icon-s-shop",list,"store");
            menus.add(menu);
        }
        if(authorityList.contains("商品管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("3-1","商品管理","/commodity","commodity");
            list.add(childrenMenu);
            Menu menu = new Menu("3","商品管理","el-icon-s-goods",list,"commodity");
            menus.add(menu);
        }
        if(authorityList.contains("订单管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu1 = new ChildrenMenu("4-1","商品订单","/commodityOrder","commodityOrder");
            ChildrenMenu childrenMenu2 = new ChildrenMenu("4-2","服务订单","/serviceOrder","serviceOrder");
            list.add(childrenMenu1);
            list.add(childrenMenu2);
            Menu menu = new Menu("4","订单管理","el-icon-s-order",list,"order");
            menus.add(menu);
        }
        if(authorityList.contains("服务管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("5-1","服务管理","/service","service");
            list.add(childrenMenu);
            Menu menu = new Menu("5","服务管理","el-icon-phone",list,"service");
            menus.add(menu);
        }
        if(authorityList.contains("宠主管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("6-1","宠主管理","/master","master");
            list.add(childrenMenu);
            Menu menu = new Menu("6","宠主管理","el-icon-s-custom",list,"master");
            menus.add(menu);
        }
        if(authorityList.contains("用户管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("7-1","用户管理","/user","user");
            list.add(childrenMenu);
            Menu menu = new Menu("7","用户管理","el-icon-user-solid",list,"user");
            menus.add(menu);
        }
        if(authorityList.contains("角色管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("8-1","角色管理","/role","role");
            list.add(childrenMenu);
            Menu menu = new Menu("8","角色管理","el-icon-user",list,"role");
            menus.add(menu);
        }
        if(authorityList.contains("权限管理权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu = new ChildrenMenu("9-1","权限管理","/authority","authority");
            list.add(childrenMenu);
            Menu menu = new Menu("9","权限管理","el-icon-lock",list,"authority");
            menus.add(menu);
        }
        if(authorityList.contains("统计权限")){
            List<ChildrenMenu> list = new ArrayList<>();
            ChildrenMenu childrenMenu1 = new ChildrenMenu("10-1","销售统计","/sale","sale");
            ChildrenMenu childrenMenu2 = new ChildrenMenu("10-2","销售额占比","/money","money");
            ChildrenMenu childrenMenu3 = new ChildrenMenu("10-3","门店分布","/location","location");
            list.add(childrenMenu1);
            list.add(childrenMenu2);
            list.add(childrenMenu3);
            Menu menu = new Menu("10","统计","el-icon-s-data",list,"Statistics");
            menus.add(menu);
        }
        return  menus;
    }


    @Override
    public User login(User u) {
        List<String> authorityList = new ArrayList<>();
        List<Role>roles = new ArrayList<>();
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserMapper.findByUserAccountAndUserPassword";
        User user = sqlSession.selectOne(stmt, u);
        if(user==null){
            return u;
        }
        int [] roleIds = getRoleIds(user.getUserId());

        for (int i = 0; i < roleIds.length; i++) {
            String stmt1 = "com.lovo.dao.RoleMapper.selectByPrimaryKey";
            Role role = sqlSession.selectOne(stmt1, roleIds[i]);
            int[] authorityIds = rs.getAuthorityIds(role.getRoleId());
            List<Authority>authorities = new ArrayList<>();
            for (int j = 0; j < authorityIds.length; j++) {
                String stmt2 = "com.lovo.dao.AuthorityMapper.selectByPrimaryKey";
                Authority authority = sqlSession.selectOne(stmt2, authorityIds[j]);
                authorities.add(authority);
                authorityList.add(authority.getAuthorityName());
            }
            role.setAuthorities(authorities);
            roles.add(role);
        }
        List<Menu>menus = getMenuList(authorityList);
        user.setRoles(roles);
        user.setMenus(menus);
        sqlSession.commit();
        sqlSession.close();
        return user;
    }

    @Override
    public User getUserById(int userId) {
        List<String> authorityList = new ArrayList<>();
        List<Role>roles = new ArrayList<>();
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserMapper.selectByPrimaryKey";
        User user = sqlSession.selectOne(stmt, userId);
        int [] roleIds = getRoleIds(user.getUserId());
        for (int i = 0; i < roleIds.length; i++) {
            String stmt1 = "com.lovo.dao.RoleMapper.selectByPrimaryKey";
            Role role = sqlSession.selectOne(stmt1, roleIds[i]);
            int[] authorityIds = rs.getAuthorityIds(role.getRoleId());
            List<Authority>authorities = new ArrayList<>();
            for (int j = 0; j < authorityIds.length; j++) {
                String stmt2 = "com.lovo.dao.AuthorityMapper.selectByPrimaryKey";
                Authority authority = sqlSession.selectOne(stmt2, authorityIds[j]);
                authorities.add(authority);
                authorityList.add(authority.getAuthorityName());
            }
            role.setAuthorities(authorities);
            roles.add(role);
        }
        List<Menu>menus = getMenuList(authorityList);
        user.setRoles(roles);
        user.setMenus(menus);
        sqlSession.commit();
        sqlSession.close();
        return user;
    }

    @Override
    public PageInfo<User> listByPage(int pageNo,int pageSize ,User user) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.UserMapper.findAllByUserAccountAndUserEmailAndUserTelAndUserName";
        PageHelper.startPage(pageNo,pageSize);
        List<User> list = sqlSession.selectList(stmt, user);
        PageInfo<User>userPageInfo = new PageInfo<>(list);
        sqlSession.commit();
        sqlSession.close();
        return userPageInfo;
    }
}
